import Vue from 'vue';
import Vuex from 'vuex';
import * as actions from './actions';
import * as mutations from './mutations';
import * as getters from './getters';
import {getToken} from '@/utils/auth';
// import {setToken, getToken, removeToken} from '@/utils/auth';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: getToken(), // token
    user: '', // 用户对象
    isLoading: false, // 是否展示loading动画
    orderBy: 'sale', // 根据什么字段排序
    signUpStep: 0, // 登陆步骤
    marketing: { // 营销
      CarouselItems: [], // 轮播图
      activity: [] // 活动
    },
    seckills: { // 秒杀
      deadline: {
        hours: 0,
        minute: 0,
        seconds: 0
      },
      goodsList: []
    },
    userInfo: { // 用户信息
      username: '',
      img: ''
    },
    storeId: '1001',
    serviceItems: [],
    eat: {}, // 爱吃专栏
    asItems: [], // 广告
    goodsList: [], // 商品列表
    shoppingCart: [], // 购物车
    couponList: [],
    couponCenterList: [],
    storeList: [],
    storeInfo: {},
    pointShop: [],
    orderList: [],
    newShoppingCart: [], // 刚加入的购物车，作为展示
    recommend: [] // 推荐购买
  },
  getters,
  actions,
  mutations
});

// export const store = new Vuex.Store({
//   state: {
//     token: getToken(), // token
//     user: '' // 用户对象
//   },
//
//   actions: {
//     login ({commit}, userInfo) {
//       const {account, password, rememberMe} = userInfo;
//       return new Promise((resolve, reject) => {
//         console.log(account);
//         console.log(password);
//         console.log(rememberMe);
//         actions.userLogin({account: account.trim(), password: password, rememberMe: rememberMe}).then(response => {
//           const {data} = response;
//           commit('SET_TOKEN_STATE', data.token);
//           setToken(data.token);
//           resolve();
//         }).catch(error => {
//           reject(error);
//         });
//       });
//     }
//   },
//
//   mutations: {
//     SET_TOKEN_STATE: (state, token) => {
//       state.token = token;
//     }
//   },
//
//   // 获取用户信息
//   getInfo ({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       actions.getUserInfo().then(response => {
//         const { data } = response;
//         if (!data) {
//           commit('SET_TOKEN_STATE', '');
//           commit('SET_USER_STATE', '');
//           removeToken();
//           resolve();
//           reject(new Error('Verification failed, please Login again.'));
//         }
//         commit('SET_USER_STATE', data);
//         resolve(data);
//       }).catch(error => {
//         reject(error);
//       });
//     });
//   },
//   // 注销
//   logout ({ commit, state }) {
//     return new Promise((resolve, reject) => {
//       actions.userlogout(state.token)
//         .then((response) => {
//           console.log(response);
//           commit('SET_TOKEN_STATE', '');
//           commit('SET_USER_STATE', '');
//           removeToken();
//           resolve();
//         })
//         .catch((error) => {
//           reject(error);
//         });
//     });
//   },
//
//   getters: {
//     token: state => state.user.token, // token
//     user: state => state.user.user // 用户对象
//   }
// });
